"""
可观察对象模型
"""

class ObservableObject:
    """可观察对象基类，实现属性变更通知机制"""
    
    def __init__(self):
        self._properties = {}
        self._observers = {}
    
    def set_property(self, name, value):
        """设置属性值"""
        old_value = self._properties.get(name)
        self._properties[name] = value
        
        # 通知属性变更
        if old_value != value:
            self.notify_property_changed(name, old_value, value)
    
    def get_property(self, name):
        """获取属性值"""
        return self._properties.get(name)
    
    def notify_property_changed(self, name, old_value, new_value):
        """通知属性变更"""
        if name in self._observers:
            for callback in self._observers[name]:
                callback(name, old_value, new_value)
    
    def bind_property(self, name, callback):
        """绑定属性变更回调"""
        if name not in self._observers:
            self._observers[name] = []
        self._observers[name].append(callback)
    
    def unbind_property(self, name, callback=None):
        """解除属性绑定"""
        if name in self._observers:
            if callback:
                if callback in self._observers[name]:
                    self._observers[name].remove(callback)
            else:
                del self._observers[name]